﻿using System;

using Sustainalytics.Portfolios.BL.Model;
using Sustainalytics.Entities;

namespace Sustainalytics.Portfolios.Service.Models
{
    public class PortfolioDetailsBaseDto
    {
        public Guid Id { get; set; }

        public Guid PortfolioId { get; set; }

        public Guid CompanyId { get; set; }

        public string CompanyNameUrlFriendly { get; set; }

        public string CompanyNameLowercase { get; set; }

        public DisplayInformationDto DisplayInfo { get; set; }

        public MatchedInformationDto MatchedInfo { get; set; }

        public double? SecurityWeight { get; set; }
        public bool IsInvalidMatch { get; set; }

        public bool IsAccesible
        {
            get
            {
                return string.IsNullOrEmpty(this.DisplayInfo.AccessDetails);
            }
        }
    }

    public class PortfolioDetailsDto : PortfolioDetailsBaseDto
    {
        public string SecurityId { get; set; }
    }

    public class PortfolioMatchesDto : PortfolioDetailsBaseDto
    {
        public string SecurityId { get; set; }
    }

    public static class TransformationExtension
    {
        public static PortfolioDetailsBaseDto ToBaseDto(this PortfolioSecurityModel portfolio)
        {
            return portfolio.ToDto();
        }

        public static PortfolioDetailsDto ToDto(this PortfolioSecurityModel portfolio)
        {
            var result = new PortfolioDetailsDto();
            result.Id = portfolio.Id;
            result.CompanyId = portfolio.CompanyId;

            result.CompanyNameUrlFriendly = portfolio.CompanyNameUrlFriendly;
            result.PortfolioId = portfolio.PortfolioId;

            result.SecurityId = portfolio.SecurityId;

            result.MatchedInfo = new MatchedInformationDto
                                     {
                                         CapitalIQId = portfolio.MatchedInfo.CapitalIQId, 
                                         CompanyName = portfolio.MatchedInfo.CompanyName
                                     };

            result.DisplayInfo = new DisplayInformationDto
                                    {
                                        SecurityIdentifier =
                                             portfolio.DisplayInfo.SecurityIdentifier, 
                                        CustomSecurityIdentifierName =
                                             portfolio.DisplayInfo.CustomSecurityIdentifierName, 
                                        AccessDetails = portfolio.AccessDetails,
                                    };
            result.SecurityWeight = portfolio.SecurityWeight;
            result.IsInvalidMatch = portfolio.IsInvalidMatch;
            return result;
        }

        public static PortfolioMatchesDto ToMatchesDto(this PortfolioSecurityModel portfolio)
        {
            var result = new PortfolioMatchesDto();
            result.Id = portfolio.Id;
            result.CompanyId = portfolio.CompanyId;

            result.CompanyNameUrlFriendly = portfolio.CompanyNameUrlFriendly;
            result.PortfolioId = portfolio.PortfolioId;
            result.SecurityWeight = portfolio.SecurityWeight;
            result.IsInvalidMatch = portfolio.IsInvalidMatch;
            result.SecurityId = portfolio.SecurityId;

            result.MatchedInfo = new MatchedInformationDto
            {
                CapitalIQId = portfolio.MatchedInfo.CapitalIQId,
                CompanyName = portfolio.MatchedInfo.CompanyName
            };

            result.DisplayInfo = new DisplayInformationDto
            {
                SecurityIdentifier = portfolio.DisplayInfo.SecurityIdentifier,
                CustomSecurityIdentifierName = portfolio.DisplayInfo.CustomSecurityIdentifierName,
                AccessDetails = portfolio.AccessDetails,
            };

            return result;
        }
    }
}